package com.benyq.networkprogramme.udp

import java.net.DatagramSocket
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import android.R.attr.data
import com.benyq.networkprogramme.base.bean.MsgEvent
import org.greenrobot.eventbus.EventBus
import java.net.DatagramPacket
import java.lang.reflect.Array.getLength




/**
 * @author benyq
 * @date 18-9-27 19:17
 * @E-mail： 1520063035@qq.com
 * @description
 */
object UDPServer {

    private var executorService: ExecutorService = Executors.newSingleThreadExecutor()
    private val port = 8888

    fun startServer(){
        executorService.execute {
            val datagramSocket = DatagramSocket (port)
            while (true){
                val data = ByteArray(1024)
                val packet = DatagramPacket(data, data.size)
                datagramSocket.receive(packet)
                val result = String(packet.data, packet.offset, packet.length)
                EventBus.getDefault().post(MsgEvent("Susan", result))
            }
        }
    }

}